#!/bin/sh

# gui package manager for Grafpup Linux
# May 2008 by Nathan Fisher for Grafpup Linux
# This program is free software released under the GNU public license version 2

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"


#######################################################################
#
# Begin with globals
#
export PKGINFODIR="/var/log/packages"
export PKGDB="${PKGINFODIR}/pkgdb.txt"
export CACHE="${PKGINFODIR}/cache"
export REPOLIST="${PKGINFODIR}/repositories.txt"
export SPECDIR="${PKGINFODIR}/spec"
export FILESDIR="${PKGINFODIR}/files"
export CDLIST="${PKGINFODIR}/cdpkgs.txt"
export VERSION="`cat $APPDIR/AppInfo.xml | grep '<Version>' | cut -f 2 -d '>' | cut -f 1 -d '<'`"

# source the configuration file
. $APPDIR/gpkg.conf

# Get translations
if [ $LANGUAGE = auto ]; then
	ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
	case $ENVIRONMENT_LANG in
		de)	export LANGUAGE="german" ;;
		en)	export LANGUAGE="english" ;;
		es)	export LANGUAGE="spanish" ;;
		fr)	export LANGUAGE="french" ;;
		it)	export LANGUAGE="italian" ;;
		nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
		nl)	export LANGUAGE="dutch" ;;
		nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
		ru)	export LANGUAGE="russian" ;;
		*)	export LANGUAGE="english" ;; #english for contries without translation
	esac
fi
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null
#
# End global declarations
#
#########################

buildlist_noscan() {
# First zero out all lists
for CAT in ALL base console desktop development doc games graphics language libraries media net office system X11
do
 echo -n "" > ${CACHE}/gpkg-list-${CAT}.txt
 echo -n "" > ${CACHE}/gpkg-on-${CAT}.txt
done
echo -n "" > ${CACHE}/gpkg-installqueue.txt
echo -n "" > ${CACHE}/gpkg-removequeue.txt
if [ "$SPLASH" = "true" ];then
 echo 'for NUM in `seq 0 1 100`; do echo $NUM; usleep 100000; done' > /tmp/splashcount
 export GPKG_SPLASH="
 <window title=\"$LOC_SPLASH\" window-position=\"1\" resizable=\"false\" icon_name=\"gtk-info\">
  <vbox>
   <hbox>
    <pixmap>
     <input file>/usr/share/icons/Tango/scalable/apps/system-installer.svg</input>
    </pixmap>
    <text>
     <label>\"$LOC_QUERY\"</label>
    </text>
   </hbox>
   <progressbar>
    <input>sh /tmp/splashcount</input>
   </progressbar>
  </vbox>
 </window>
 "
 gtkdialog3 --program GPKG_SPLASH &
 X1PID=$!
 echo "$X1PID" > ${CACHE}/splashpid.txt
fi
while read PKG
do
 PKGNAME="`cat ${CACHE}/pkgs.txt | grep "$PKG" | cut -f 2 -d '|'`"
 CAT="`cat ${CACHE}/pkgs.txt | grep "$PKG" | cut -f 5 -d '|'`"
 if [ -f "${FILESDIR}/${PKGNAME}.files" ];then
  if [ "$SHOW_DESC" = "true" ];then
   echo $PKG | cut -f 1-4 -d '|' >> ${CACHE}/gpkg-on-${CAT}.txt
   echo $PKG | cut -f 1-4 -d '|' >> ${CACHE}/gpkg-on-ALL.txt
  else
   echo $PKG | cut -f 1,2,4 -d '|' >> ${CACHE}/gpkg-on-${CAT}.txt
   echo $PKG | cut -f 1,2,4 -d '|' >> ${CACHE}/gpkg-on-ALL.txt
  fi
 else
  if [ "$SHOW_DESC" = "true" ];then
   echo $PKG | cut -f 1-4 -d '|' >> ${CACHE}/gpkg-list-${CAT}.txt
   echo $PKG | cut -f 1-4 -d '|' >> ${CACHE}/gpkg-list-ALL.txt
  else
   echo $PKG | cut -f 1,2,4 -d '|' >> ${CACHE}/gpkg-list-${CAT}.txt
   echo $PKG | cut -f 1,2,4 -d '|' >> ${CACHE}/gpkg-list-ALL.txt
  fi
 fi
done < ${CACHE}/pkgs.txt
cp -f ${CACHE}/gpkg-list-ALL.txt ${CACHE}/gpkg-list.txt
cp -f ${CACHE}/gpkg-list-ALL.txt ${CACHE}/gpkg-search-av.txt
cp -f ${CACHE}/gpkg-on-ALL.txt ${CACHE}/gpkg-on.txt
cp -f ${CACHE}/gpkg-on-ALL.txt ${CACHE}/gpkg-search-on.txt
}

buildlist() {
# First zero out all lists
for CAT in ALL base console desktop development doc games graphics language libraries media net office system X11
do
 echo -n "" > ${CACHE}/gpkg-list-${CAT}.txt
 echo -n "" > ${CACHE}/gpkg-on-${CAT}.txt
 #echo -n "" > ${CACHE}/pkgs.txt
done
echo -n "" > ${CACHE}/gpkg-installqueue.txt
echo -n "" > ${CACHE}/gpkg-removequeue.txt
if [ "$SPLASH" = "true" ];then
 echo 'for NUM in `seq 0 1 100`; do echo $NUM; usleep 100000; done' > /tmp/splashcount
 export GPKG_SPLASH="
 <window title=\"$LOC_SPLASH\" window-position=\"1\" resizable=\"false\" icon_name=\"gtk-info\">
  <vbox>
   <hbox>
    <pixmap>
     <input file>/usr/share/icons/Tango/scalable/apps/system-installer.svg</input>
    </pixmap>
    <text>
     <label>\"$LOC_QUERY\"</label>
    </text>
   </hbox>
   <progressbar>
    <input>sh /tmp/splashcount</input>
   </progressbar>
  </vbox>
 </window>
 "
 gtkdialog3 --program GPKG_SPLASH &
 X1PID=$!
 echo "$X1PID" > ${CACHE}/splashpid.txt
fi

for PKG in `ls $SPECDIR`
do
 CATEGORY="`cat ${SPECDIR}/${PKG} | grep CATEGORY= | cut -f 2 -d '"' | head -n 1`" #'
 case $CATEGORY in
  base)
  ICON="emblem-important"
  ;;
  console)
  ICON="utilities-terminal"
  ;;
  desktop)
  ICON="preferences-system-windows"
  ;;
  development)
  ICON="applications-development"
  ;;
  doc)
  ICON="help-browser"
  ;;
  games)
  ICON="applications-games"
  ;;
  graphics)
  ICON="applications-graphics"
  ;;
  language)
  ICON="preferences-desktop-locale"
  ;;
  libraries)
  ICON="application-x-executable"
  ;;
  media)
  ICON="applications-multimedia"
  ;;
  net)
  ICON="network-server"
  ;;
  office)
  ICON="applications-office"
  ;;
  system)
  ICON="applications-system"
  ;;
  X11)
  ICON="video-display"
  ;;
 esac
 PKGNAME="`cat ${SPECDIR}/${PKG} | grep NAME= | cut -f 2 -d '"'`" #'
 if [ "$SHOW_DESC" = "true" ];then
  DESC="`cat ${SPECDIR}/${PKG} | grep DESC= | cut -f 2 -d '"' | cut -b 1-30`|" #'
 fi
 #DESCRIPTION="`cat ${SPECDIR}/${PKG} | grep DESC= | cut -f 2 -d '"'`" #'
 SIZE="`cat ${SPECDIR}/${PKG} | grep SIZE= | cut -f 2 -d '"'`" #'
 #echo "${ICON}|${PKGNAME}|${DESCRIPTION}|${SIZE}|${CATEGORY}" >> ${CACHE}/pkgs.txt
 if [ ! -f "${FILESDIR}/${PKGNAME}.files" ];then
  echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-list-ALL.txt
  echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-list-${CATEGORY}.txt
 else
  echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-on-ALL.txt
  echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-on-${CATEGORY}.txt
 fi
done
}

view_category() {
CATEGORY="$1"
cp -f ${CACHE}/gpkg-list-${CATEGORY}.txt ${CACHE}/gpkg-list.txt
cp -f ${CACHE}/gpkg-on-${CATEGORY}.txt ${CACHE}/gpkg-on.txt
}

add_to_queue() {
PKGNAME="$1"
SPEC=${SPECDIR}/${PKGNAME}.spec
DESC=""
if [ "$SHOW_DESC" = "true" ];then
 DESC="`cat $SPEC | grep DESC= | cut -f 2 -d '"'`|" #'
fi
SIZE="`cat $SPEC | grep SIZE= | cut -f 2 -d '"'`" #'
CATEGORY="`cat $SPEC | grep CATEGORY= | cut -f 2 -d '"' | head -n 1`" #'
case $CATEGORY in
 base)
 ICON="emblem-important"
 ;;
 console)
 ICON="utilities-terminal"
 ;;
 desktop)
 ICON="preferences-system-windows"
 ;;
 development)
 ICON="applications-development"
 ;;
 doc)
 ICON="help-browser"
 ;;
 games)
 ICON="applications-games"
 ;;
 graphics)
 ICON="applications-graphics"
 ;;
 language)
 ICON="preferences-desktop-locale"
 ;;
 libraries)
 ICON="application-x-executable"
 ;;
 media)
 ICON="applications-multimedia"
 ;;
 net)
 ICON="network-server"
 ;;
 office)
 ICON="applications-office"
 ;;
 system)
 ICON="applications-system"
 ;;
 X11)
 ICON="video-display"
 ;;
esac
if [ "`cat ${CACHE}/gpkg-installqueue.txt | grep $PKGNAME`" = "" ];then
 if [ ! "$PKGNAME" = "process" ];then
  echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-installqueue.txt
 fi
fi
}

slot_removal() {
PKGNAME="$1"
[ "$PKGNAME" = "" ] && exit 0
SPEC=${SPECDIR}/${PKGNAME}.spec
DESC=""
if [ "$SHOW_DESC" = "true" ];then
 DESC="`cat $SPEC | grep DESC= | cut -f 2 -d '"'`|" #'
fi
SIZE="`cat $SPEC | grep SIZE= | cut -f 2 -d '"'`" #'
CATEGORY="`cat $SPEC | grep CATEGORY= | cut -f 2 -d '"' | head -n 1`" #'
case $CATEGORY in
 base)
 ICON="emblem-important"
 ;;
 console)
 ICON="utilities-terminal"
 ;;
 desktop)
 ICON="preferences-system-windows"
 ;;
 development)
 ICON="applications-development"
 ;;
 doc)
 ICON="help-browser"
 ;;
 games)
 ICON="applications-games"
 ;;
 graphics)
 ICON="applications-graphics"
 ;;
 language)
 ICON="preferences-desktop-locale"
 ;;
 libraries)
 ICON="application-x-executable"
 ;;
 media)
 ICON="applications-multimedia"
 ;;
 net)
 ICON="network-server"
 ;;
 office)
 ICON="applications-office"
 ;;
 system)
 ICON="applications-system"
 ;;
 X11)
 ICON="video-display"
 ;;
esac
if [ "`cat ${CACHE}/gpkg-removequeue.txt | grep $PKGNAME`" = "" ];then
 echo "${ICON}|${PKGNAME}|${DESC}${SIZE}" >> ${CACHE}/gpkg-removequeue.txt
fi
}

# Show package info
show_info() {
export INFO_DLG="
<window title=\"$LOC_INFO\" icon_name=\"gtk-info\">
 <vbox>
  <notebook labels=\"$LOC_SPEC|$LOC_DEPS\">
   <vbox>
    <list>
     <width>400</width>
     <height>150</height>
     <input>cat $SPECDIR/$1.spec | grep -v === | grep -v depends | tr -s \"\n\"</input>
    </list>
   </vbox>
   <vbox>
    <list>
     <input>cat $SPECDIR/$1.spec | grep depends | cut -f 2 -d ' '</input>
    </list>
   </vbox>
  </notebook>
  <hbox>
   <button>
    <input file stock=\"gtk-close\"></input>
    <label>$LOC_CLOSE</label>
   </button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program INFO_DLG
}

# Search installed packages
search_on() {
# Zero put search
echo -n "" > ${CACHE}/gpkg-on-search.txt
SEARCH_TEXT="$1"
for LINE in `cat ${CACHE}/gpkg-search-on.txt | tr ' ' '%'`
do
 PKGNAME="`echo $LINE | cut -f 1 -d '|'`"
 if [ ! "`echo $PKGNAME | grep -i "$SEARCH_TEXT"`" = "" ];then
  echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-on-search.txt
 else
  if [ ! "`echo $LINE | cut -f 2-3 -d '|' | grep -i "$SEARCH_TEXT"`" = "" ];then
   echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-on-search.txt
  else
   if [ ! "`cat ${SPECDIR}/${PKGNAME}.spec | grep depends | grep -i "$SEARCH_TEXT"`" = "" ];then
    echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-on-search.txt
   fi
  fi
 fi
done
cp -f ${CACHE}/gpkg-on-search.txt ${CACHE}/gpkg-on.txt
}

# Search available packages #$APPDIR/functions -search-av \$CATEGORY \$SEARCH_AV
search_av() {
# Zero put search
echo -n "" > ${CACHE}/gpkg-list-search.txt
SEARCH_TEXT="$1"
for LINE in `cat ${CACHE}/gpkg-search-av.txt | tr ' ' '%'`
do
 PKGNAME="`echo $LINE | cut -f 1 -d '|'`"
 if [ ! "`echo $PKGNAME | grep -i "$SEARCH_TEXT"`" = "" ];then
  echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-list-search.txt
 else
  if [ ! "`echo $LINE | cut -f 2-3 -d '|' | grep -i "$SEARCH_TEXT"`" = "" ];then
   echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-list-search.txt
  else
   if [ ! "`cat ${SPECDIR}/${PKGNAME}.spec | grep depends | grep -i "$SEARCH_TEXT"`" = "" ];then
    echo $LINE | tr '%' ' ' >> ${CACHE}/gpkg-list-search.txt
   fi
  fi
 fi
done
cp -f ${CACHE}/gpkg-list-search.txt ${CACHE}/gpkg-list.txt
}

# Options
gpkg_options() {
LANGUAGES=""
for PO in `ls $APPDIR/locale | sed 's/.po//g'`
do
 LANGUAGES="$LANGUAGES<item>$PO</item>"
done
export OPTIONS_DLG="
<window title=\"$LOC_OPTIONSTITLE\" icon_name=\"gtk-preferences\">
 <vbox>
  <hbox>
   <checkbox>
    <label>$LOC_CONFIRM</label>
    <variable>CONFIRM_ACTIONS</variable>
    <default>$CONFIRM_ACTIONS</default>
   </checkbox>
  </hbox>
  <hbox>
   <checkbox>
    <label>$LOC_SHOWDESC</label>
    <variable>SHOW_DESC</variable>
    <default>$SHOW_DESC</default>
   </checkbox>
  </hbox>
  <hbox>
   <checkbox>
    <label>$LOC_SHOWLOG</label>
    <variable>SHOW_LOG</variable>
    <default>$SHOW_LOG</default>
   </checkbox>
  </hbox>
  <hbox>
   <checkbox>
    <label>$LOC_SHOWSPLASH</label>
    <variable>SPLASH</variable>
    <default>$SPLASH</default>
   </checkbox>
  </hbox>
  <hbox>
   <text>
    <label>$LOC_LANGUAGE</label>
   </text>
   <combobox>
    <variable>LANGUAGE</variable>
    <item>auto</item>
    $LANGUAGES
   </combobox>
  </hbox>
  <hbox>
   <button>
    <input file stock=\"gtk-close\"></input>
    <label>$LOC_CLOSE</label>
   </button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program OPTIONS_DLG > ${APPDIR}/gpkg.conf
}

# Select and install a local package
install_local_pkg() {
export PKG_CHOOSER="
<window title=\"$LOC_CHOOSER\" icon_name=\"system-installer\">
 <vbox>
  <chooser>
   <height>500</height><width>600</width>
   <variable>PACKAGE</variable>
   <action>EXIT:ok</action>
  </chooser>
  <hbox>
   <button>
    <input file stock=\"gtk-cancel\"></input>
    <label>$LOC_CANCEL</label>
   </button>
   <button>
    <input file stock=\"gtk-ok\"></input>
    <label>$LOC_OK</label>
    <action>EXIT:ok</action>
   </button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program PKG_CHOOSER > /tmp/gpkg-exit.txt
. /tmp/gpkg-exit.txt
rm -f /tmp/gpkg-exit.txt
if [ ! "$EXIT" = "ok" ];then
 exit 0
fi
[ "`echo $PACKAGE | grep .tbz`" = "" ] && exit 1
PKGNAME="`basename $PACKAGE .tbz`"
ln -s "$PACKAGE" ${CACHE}/${PKGNAME}.tbz
DIR="`pwd`"
cd $CACHE
export STATUS_DLG="<window title=\"Installing $PKGNAME\" window_position=\"2\">
 <vbox>
  <text>
   <label>Installing $PKGNAME, please stand by</label>
  </text>
  <progressbar>
   <label>Installing...</label>
   <input>installpkg ${PKGNAME} -q && echo 100</input>
   <action type=\"exit\">Ready</action>
  </progressbar>
 </vbox>
</window>"
gtkdialog3 --program STATUS_DLG
pkg process $PKGNAME
cat ${CACHE}/installqueue | grep -v $PKGNAME > ${CACHE}/installqueue.new
mv -f ${CACHE}/installqueue.new ${CACHE}/installqueue
[ "`cat ${CACHE}/installqueue`" = "" ] && exit 0
echo -n "" > ${CACHE}/gpkg-installqueue.txt
echo -n "" > ${CACHE}/removequeue
for PKG in `cat ${CACHE}/installqueue`
do
 $APPDIR/functions -add-to-queue $PKG
done
confirm_actions
rm -f ${PKGNAME}.tbz
cd "$DIR"
}

# Update the package database
update_pkg_db() {
echo "update function"
}

# About window
tell_about() {
export ABOUT_DLG="<window title=\"Gpkg: about\" window_position=\"2\" icon_name=\"gtk-about\" resizable=\"false\">
 <vbox>
  <notebook labels=\"About|License|Credits\">
   <vbox>
    <pixmap>
     <input file>$APPDIR/images/system-installer.svg</input>
    </pixmap>
    <text use_markup=\"true\">
     <label>\"<b>Gpkg $VERSION</b>\"</label>
    </text>
    <text>
     <label>A graphical package manager for Grafpup Linux.</label>
    </text>
    <text>
     <label>Nathan Fisher, Copyright © 2008</label>
    </text>
    <text><label>\"\"</label></text>
    <button>
     <label>http://grafpup.org/docs/package-management.php</label>
     <action>defaultbrowser http://grafpup.org/docs/package-management.php &</action>
    </button>
   </vbox>
   <vbox>
    <edit editable=\"false\">
     <default>\"Gpkg is free software; you can redistribute it
and/or modify it under the terms of the GNU
General Public License as published by the
Free Software Foundation; either version 2 of
the License, or (at your option) any later
version.

Gpkg is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of
MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU
General Public License along with Gpkg; if not,
write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA
02111-1307, USA.\"</default>
    </edit>
    <button>
     <label>\"http://www.gnu.org/licenses/\"</label>
     <action>defaultbrowser http://www.gnu.org/licenses/ &</action>
    </button>
   </vbox>
   <vbox>
    <edit editable=\"false\">
     <default>\"Translations:
Frech:			Jean-Jacques Moulinier
Italian:			Angello Gemmi

Written summer 2008 by Nathan Fisher.
Portions of the code are inspired by
PETget, by Barry Kauler.\"</default>
    </edit>
   </vbox>
  </notebook>
  <hbox>
   <button>
    <input file stock=\"gtk-close\"></input>
    <label>$LOC_CLOSE</label>
   </button>
  </hbox>
 </vbox>
</window>"
gtkdialog3 --program ABOUT_DLG
}

# Kill the splash window
kill_splash() {
X1PID="`cat ${CACHE}/splashpid.txt`"
[ ! "$X1PID" = "" ] && kill $X1PID
}

# Apply all changes
apply_changes() {
NUM2ADD="`cat ${CACHE}/installqueue | wc -l | tr -d ' '`"
NUM2REMOVE="`cat ${CACHE}/removequeue | wc -l | tr -d ' '`"
NUMOPS="`expr $NUM2ADD + $NUM2ADD + $NUM2REMOVE`"
BARSTAT="`expr 100 % $NUMOPS`"
BARJUMP="`expr 100 / $NUMOPS`"
echo $BARSTAT
NUMPKG="1"
for PKG in `cat ${CACHE}/installqueue`
do
 echo "$LOC_FETCHING $NUMPKG $LOC_OF $NUM2ADD ${PKG}.tbz"
 pkg-quiet fetch $PKG >> ${CACHE}/pkg.log
 BARSTAT="`expr $BARSTAT + $BARJUMP`"
 echo $BARSTAT
 NUMPKG="`expr $NUMPKG + 1`"
done
NUMPKG="1"
for PKG in `cat ${CACHE}/installqueue`
do
 echo "$LOC_INSTALLING $NUMPKG $LOC_OF $NUM2ADD $PKG"
 installpkg ${CACHE}/${PKG} -q >> ${CACHE}/pkg.log
 rm -f ${CACHE}/${PKG}.tbz
 BARSTAT="`expr $BARSTAT + $BARJUMP`"
 echo $BARSTAT
 NUMPKG="`expr $NUMPKG + 1`"
done
NUMPKG="1"
for PKG in `cat ${CACHE}/removequeue`
do
 echo "$LOC_REMOVING $NUMPKG $LOC_OF $NUM2REMOVE $PKG"
 removepkg ${PKG} -q >> ${CACHE}/pkg.log
 BARSTAT="`expr $BARSTAT + $BARJUMP`"
 echo $BARSTAT
 NUMPKG="`expr $NUMPKG + 1`"
done
if [ -f "${CACHE}/logpid" ];then
 LOGPID=`cat ${CACHE}/logpid`
 kill $LOGPID
 rm -f ${CACHE}/logpid
 fixmenus
fi
touch $CACHE/rescan.txt
}

# Run the progress bar while applying changes
apply_window() {
export APPLY_BAR="
<window title=\"$LOC_APPLYWINDOW\" icon_name=\"gtk-execute\" window-position=\"1\">
 <vbox>
  <text>
   <label>$LOC_PERFORMING</label>
  </text>
  <progressbar>
   <label>$LOC_BEGIN</label>
   <action  type=\"exit\">READY</action>
   <input>$APPDIR/functions -apply-changes</input>
  </progressbar>
  <hbox>
   <button>
    <input file icon=\"utilities-system-monitor\"></input>
    <label>\"$LOC_SHOWDET\"</label>
    <action>$APPDIR/functions -showlog</action>
   </button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program APPLY_BAR
$APPDIR/functions -buildlist
$APPDIR/functions -killsplash
}

# Confirmation window
confirm_actions() {

# Size to be added
SIZE_ADDED="0"
for PKG in `cat ${CACHE}/gpkg-installqueue.txt | cut -f 2 -d '|'`
do
 SIZE="`cat ${CACHE}/gpkg-installqueue.txt | grep $PKG | cut -f 4 -d '|' | tr -d 'K'`"
 echo $PKG $SIZE
 SIZE_ADDED="`expr $SIZE_ADDED + $SIZE`"
done
LOC_SIZE_ADD="`echo $LOC_SIZE_ADD | sed "s/SIZE2ADD/$SIZE_ADDED/"`"

# Space to be freed
SIZE_FREED="0"
for PKG in `cat ${CACHE}/gpkg-removequeue.txt | cut -f 2 -d '|'`
do
 SIZE="`cat ${CACHE}/gpkg-removequeue.txt | grep $PKG | cut -f 4 -d '|' | tr -d 'K'`"
 SIZE_FREED="`expr $SIZE_FREED + $SIZE`"
done
LOC_SIZE_FREED="`echo $LOC_SIZE_FREED | sed "s/SIZEFREED/$SIZE_FREED/"`"

export CONF_DLG="<window title=\"$LOC_CONF_TITLE\" icon_name=\"gtk-dialog-question\" window-position=\"1\">
 <vbox>
  <text>
   <label>$LOC_CONF_HINT</label>
  </text>
  <notebook labels=\"$LOC_CONF_TABS\">
   <vbox>
    <frame>
     <tree>
      <height>150</height>
      <variable>PKGS2ADD</variable>
      <label>$LOC_CONF_HEADER</label>
      <input icon_column=\"0\">cat ${CACHE}/gpkg-installqueue.txt | cut -f 1,2,4 -d '|'</input>
     </tree>
     <hbox>
      <text>
       <label>$LOC_SIZE_ADD</label>
      </text>
      <button>
       <input file stock=\"gtk-info\"></input>
       <action>$APPDIR/functions -showinfo \$PKGS2ADD &</action>
      </button>
     </hbox>
    </frame>
   </vbox>
   <vbox>
    <frame>
     <tree>
      <height>150</height>
      <variable>PKGS2REMOVE</variable>
      <label>$LOC_CONF_HEADER</label>
      <input icon_column=\"0\">cat ${CACHE}/gpkg-removequeue.txt | cut -f 1,2,4 -d '|'</input>
     </tree>
     <hbox>
      <text>
       <label>$LOC_SIZE_FREED</label>
      </text>
      <button>
       <input file stock=\"gtk-info\"></input>
       <action>$APPDIR/functions -showinfo \$PKGS2REMOVE &</action>
      </button>
     </hbox>
    </frame>
   </vbox>
  </notebook>
  <hbox>
   <button cancel></button>
   <button ok></button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program CONF_DLG > /tmp/gpkg-go
. /tmp/gpkg-go
rm -f /tmp/gpkg-go
[ ! "$EXIT" = "OK" ] && exit 0
exec $APPDIR/functions -apply-window
}

case $1 in
	-buildlist)
	if [ -f "$CACHE/rescan.txt" ];then
	 buildlist
	 rm -f $CACHE/rescan.txt
	fi
	cp -f ${CACHE}/gpkg-list-ALL.txt ${CACHE}/gpkg-list.txt
	cp -f ${CACHE}/gpkg-list-ALL.txt ${CACHE}/gpkg-search-av.txt
	cp -f ${CACHE}/gpkg-on-ALL.txt ${CACHE}/gpkg-on.txt
	cp -f ${CACHE}/gpkg-on-ALL.txt ${CACHE}/gpkg-search-on.txt
	;;
	-view-category)
	view_category $2
	;;
	-add-to-queue)
	add_to_queue $2
	;;
	-slot-removal)
	slot_removal $2
	;;
	-showinfo)
	show_info $2
	;;
	-search-on)
	ARGS="`echo $@ | cut -f 2-99 -d ' ' | tr ' ' '%'`"
	search_on "$ARGS"
	;;
	-search-av)
	ARGS="`echo $@ | cut -f 2-99 -d ' ' | tr ' ' '%'`"
	search_av "$ARGS"
	;;
	-install-local)
	install_local_pkg
	;;
	-update-db)
	update_pkg_db
	;;
	-about)
	tell_about
	;;
	-help)
	defaulthtmlviewer file://${APPDIR}/gpkg.html
	;;
	-prefs)
	gpkg_options
	;;
	-killsplash)
	kill_splash
	;;
	-apply)
    [ "$SHOW_LOG" = "true" ] && $APPDIR/functions -showlog
    sleep 1
    REMOVEQUEUE="`cat ${CACHE}/gpkg-removequeue.txt | cut -f 2 -d '|'`"
    INSTALLQUEUE="`cat ${CACHE}/gpkg-installqueue.txt | cut -f 2 -d '|'`"
    pkg process $INSTALLQUEUE >> ${CACHE}/pkg.log
    echo -n "" > ${CACHE}/gpkg-installqueue.txt
    for PKG in `cat ${CACHE}/installqueue`
    do
     $APPDIR/functions -add-to-queue $PKG
    done
    echo -n "" > ${CACHE}/removequeue
    echo -n "" > ${CACHE}/pkg.log
    for PKG in $REMOVEQUEUE
    do
     if [ "`cat ${CACHE}/installqueue | grep $PKG`" = "" ];then
      echo $PKG >> ${CACHE}/removequeue
     fi
    done
    echo -n "" > ${CACHE}/gpkg-removequeue.txt
    for PKG in `cat ${CACHE}/removequeue`
    do
     $APPDIR/functions -slot-removal $PKG
    done
	confirm_actions
	;;
	-apply-window)
	apply_window
	;;
	-apply-changes)
	apply_changes
	;;
	-showlog)
	echo -n "" > ${CACHE}/pkg.log
	[ "`ps | grep "gtklogfileviewer ${CACHE}/pkg.log" | grep -v grep`" = "" ] && gtklogfileviewer ${CACHE}/pkg.log Close 0 0 500 400
	LOGPID="`ps | grep "gtklogfileviewer ${CACHE}/pkg.log" | cut -f 1 -d ' '`"
	echo $LOGPID > ${CACHE}/logpid
	;;
esac

